﻿using System;
using System.Linq;
using Newtonsoft.Json.Linq;

namespace VkApiVer2.api
{
    class ErrorResponse
    {
        public string ErrorCode { get; set; }
        public string ErrorMessage { get; set; }
        public string CaptchaSid { get; set; }
        public string CapthcaImage { get; set; }
        public Array RequestParameters { get; set; }


        public static bool TryParseErrorResponse(JObject jObject, out ErrorResponse response)
        {
            response = new ErrorResponse();
            JToken error = jObject["error"];
            if(error != null)
            {
                response.ErrorCode = (string)error["error_code"];
                response.ErrorMessage = (string)error["error_msg"];
                if(response.ErrorCode.Equals("14"))
                {
                    response.CaptchaSid = (string)error["captcha_sid"];
                    response.CapthcaImage = (string)error["captcha_img"];
                }
                response.RequestParameters = error["request_params"].ToArray();
                return true;
            }

            response = null;
            return false;
        }
    }
}
